/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.dao.impl;

import au.edu.unsw.bookmarket.actionform.CourseBean;
import au.edu.unsw.bookmarket.dao.CourseDao;
import au.edu.unsw.bookmarket.jdo.Course;
import au.edu.unsw.bookmarket.service.PMF;
import au.edu.unsw.bookmarket.util.BeanMapping;
import javax.jdo.Query;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;

/**
 *
 * @author gongz
 */
public class CourseDaoImpl implements CourseDao {

    public void addCourseAsCourseBean(CourseBean courseBean) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        BeanMapping beanMapping = new BeanMapping();
        try {
            Course course = beanMapping.courseBean2Course(courseBean);
            pm.makePersistent(course);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pm.close();
        }
    }

    public void addCourse(Course course) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(course);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pm.close();
        }
    }

    public CourseBean courseExistency(CourseBean courseBean) {
        CourseBean course = null;
        PersistenceManager pm = PMF.get().getPersistenceManager();
        BeanMapping beanMapping = new BeanMapping();
        Query query = pm.newQuery(Course.class);
        try {
            query.setFilter("code == inputCode");
            query.declareParameters("String inputCode");
            List<Course> results = (List<Course>) query.execute(courseBean.getCode().trim().toLowerCase());
            if (results.size() > 0) {
                System.out.println("found ISBN: " + courseBean.getCode());
                return beanMapping.course2CourseBean(results.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return course;
    }

    public List<CourseBean> getAllCourses() {
        List<CourseBean> courseBeans = new ArrayList<CourseBean>();
        BeanMapping beanMapping = new BeanMapping();
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Course.class);
        try {
            List<Course> courses = (List<Course>) query.execute();
            for (Course course : courses) {
                courseBeans.add(beanMapping.course2CourseBean(course));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return courseBeans;
    }

    public boolean removeAllCourses() {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(Course.class);
        boolean success = false;
        try {
            query.deletePersistentAll();
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            query.closeAll();
        }
        return success;
    }
}
